package com.sen.organization.controller;

import com.sen.common.common.controller.BaseController;
import com.sen.common.common.response.MessageResult;
import com.sen.common.common.util.CheckUtils;
import com.sen.organization.dto.MemberPeopleDTO;
import com.sen.organization.dto.MemberPeopleSearchDTO;
import com.sen.organization.feign.MemberPeopleFeign;
import com.sen.organization.service.PeopleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 人员controller
 *
 * @author esjiang
 */
@RestController
@RequestMapping("/people")
public class PeopleController extends BaseController implements MemberPeopleFeign {

    @Resource
    PeopleService peopleService;

    /**
     * 新增人员
     *
     * @param memberPeopleDTO
     * @return
     */
    @PostMapping("/add")
    public MessageResult add(@RequestBody MemberPeopleDTO memberPeopleDTO) {
        return exec("新增人员", memberPeopleDTO, () -> {
            CheckUtils.checkNotNull(memberPeopleDTO, "base");
            peopleService.add(memberPeopleDTO);
            return MessageResult.success();
        });
    }

    /**
     * 修改人员
     *
     * @param memberPeopleDTO
     * @return
     */
    @PostMapping("/update")
    public MessageResult update(@RequestBody MemberPeopleDTO memberPeopleDTO) {
        return exec("修改人员", memberPeopleDTO, () -> {
            CheckUtils.checkNotNull(memberPeopleDTO, "base");
            CheckUtils.checkNotNull(memberPeopleDTO.getBase(), "id");
            peopleService.upd(memberPeopleDTO);
            return MessageResult.success();
        });
    }

    /**
     * 删除人员
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public MessageResult del(@RequestParam("id") Long id) {
        return exec("删除人员", id, () -> {
            CheckUtils.checkNotNull(id);
            peopleService.del(id);
            return MessageResult.success();
        });
    }

    /**
     * 查询列表
     *
     * @param searchDTO
     * @return
     */
    @PostMapping("/list")
    public MessageResult list(@RequestBody MemberPeopleSearchDTO searchDTO) {
        return exec("查询人员列表", searchDTO, () -> MessageResult.success(peopleService.list(searchDTO)));
    }

    /**
     * 查询人员信息
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public MessageResult get(@RequestParam("id") Long id) {
        return exec("查询个人信息", id, () -> {
            CheckUtils.checkNotNull(id);
            return MessageResult.success(peopleService.getMemberPeople(id));
        });
    }

    /**
     * 账户登录-获取人员信息
     * @param userName
     * @return
     */
    @Override
    @GetMapping("/peopleInfo")
    public MessageResult getPeopleInfoByLogin(@RequestParam("userName") String userName){
        return exec("账户登录获取人员信息",userName,()->{
            CheckUtils.checkNotNull(userName);
            return MessageResult.success(peopleService.getPeopleInfo(userName)) ;
        }) ;
    }

}
